import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
part 'dialog.dart';

showAlert({
  final String? title,
  final String? msg,
  final String? cancelText = '取消',
  final String? sureText = '确定',
  final Color? cancelColor,
  final Color? sureColor,
  final VoidCallback? cancelAction,
  final VoidCallback? sureAction,
}) {
  if (Get.context == null) {
    return;
  }
  List<Widget> _getActions() {
    List<Widget> actions = [];
    if (cancelText != null) {
      actions.add(
        CupertinoDialogAction(
          onPressed: () {
            Navigator.of(Get.context!).pop();
            if (cancelAction != null) {
              Future.delayed(const Duration(milliseconds: 300), () {
                cancelAction();
              });
            }
          },
          child: Text(
            cancelText,
            style: TextStyle(
                color: cancelColor ?? const Color(0xff999999), fontSize: 13.sp),
          ),
        ),
      );
    }
    if (sureText != null) {
      actions.add(
        CupertinoDialogAction(
          onPressed: () {
            Navigator.of(Get.context!).pop();
            if (sureAction != null) {
              Future.delayed(const Duration(milliseconds: 300), () {
                sureAction();
              });
            }
          },
          child: Text(
            sureText,
            style: TextStyle(
                color: cancelColor ?? const Color(0xff43a8e8), fontSize: 13.sp),
          ),
        ),
      );
    }
    return actions;
  }

  showCupertinoDialog(
      context: Get.context!,
      barrierDismissible: true,
      builder: (context) {
        return CupertinoAlertDialog(
          title: title == null
              ? null
              : Text(
                  title,
                  style: TextStyle(
                      color: const Color(0xff333333),
                      fontSize: 18.sp,
                      fontWeight: FontWeight.bold),
                ),
          content: msg == null
              ? null
              : Text(
                  msg,
                  textAlign: TextAlign.left,
                  style: TextStyle(
                      color: const Color(0xff666666), fontSize: 15.sp),
                ),
          actions: _getActions(),
        );
      });
}
